package com.jcl.recruit.mobile.service;

import com.jcl.core.utils.StringUtils;
import com.jcl.recruit.basic.login.impl.RecruitLoginImpl;
import com.jcl.recruit.basic.login.model.UserExternalLoginInfo;
import com.jcl.recruit.console.logic.ConsoleUserAccessLogic;
import com.jcl.recruit.console.model.ConsoleUserAccess;
import com.jcl.recruit.core.constants.UserExternalLoginConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName MobileService

 * @Author yyliu
 * @Date 2018/9/19 9:24
 * @Version 1.0
 **/
@Service
public class MobileLoginService extends RecruitLoginImpl {


    @Autowired
    private ConsoleUserAccessLogic consoleUserAccessLogic;

    /* *
     * @Author yyliu
     * @Description 切换企业登录
     * @Date 16:27 2018/10/19
     * @Param [username]
     * @return void
     **/
    public UserExternalLoginInfo changeCorpLogin(String username) {
        UserExternalLoginInfo externalLoginInfo = null;
        if (StringUtils.hasLength(username)) {
            String[] arr = username.split(UserExternalLoginConstant.USER_ACCESS_SPLIT);
            String externalKey = arr[0];
            String corpCode = arr[1];
            ConsoleUserAccess userAccess = consoleUserAccessLogic.
                    findOneByExternalKeyAndCode(externalKey, corpCode);
            if (null != userAccess) {

                consoleUserAccessLogic.changeUseStatusToUnused(
                        externalKey, UserExternalLoginConstant.USER_ACCESS_CATEGORY_WECHAT);
                userAccess.setStatus(UserExternalLoginConstant.USER_ACCESS_STATUS_USED);
                consoleUserAccessLogic.updateAccess(userAccess);
                externalLoginInfo = new UserExternalLoginInfo();
                externalLoginInfo.setUsername(userAccess.getUsername());
                externalLoginInfo.setPassword(userAccess.getPassword());
                externalLoginInfo.setUserId(userAccess.getUserId());
                externalLoginInfo.setCorpCode(userAccess.getCorpCode());
            }
        }

        return externalLoginInfo;
    }

}
